# -*- encoding: utf-8 -*-
"""
测试-主函数入口
"""
import os
import pytest
import numpy as np

from application.simulator_interface import time_process

from application.input_init import input_init
from utils.optmization import error_MSE
from utils.constant import BoundaryConditionType
from tests.visual_olga_horizontal import load_olga_groundtruth_result_se
from utils.base_path import get_base_path
from utils.crdlog import log
from utils.common import get_enum_name_by_value

from tests.input.olga_case_data import *
from tests.input.game_case_data import *


# pytest传参
@pytest.mark.parametrize(
    "input_case",
    [
    # 最初olga的1-5算例：
    # input_olga1,
    # input_olga2,
    # input_olga3,
    # input_olga4,
    # input_olga5,

    # olga_gaohanye1, # 高含液 25-01-16 olga


    # 比赛 初赛 1-5测试数据 25-01-16 olga ：
    game_test_1,
    game_test_2,
    game_test_3,
    game_test_4, # TODO: 输入长度和里程一致，算例有问题
    game_test_5,


    ],
    ids=lambda x: next(name for name, val in globals().items() if val is x)
)


def test_simple(request, caplog, input_case):
    """
    simple算法测试入口
    Args:
        initdata: dict, 原始参数
    """
    current_case_name = request.node.callspec.id

    # 2. 整体迭代, 迭代过程输出结果并保存（默认不保存）

    params = time_process(input = input_case, flow_pattern_existence=False)
    # params = time_process(input=input_case, specific_end_time = 3600, flow_pattern_existence=False)

    # 3. 计算解析解，进而计算误差
    if params["boundary_class"] == BoundaryConditionType.inlet_M_outlet_p.value:
  
        gtpath_dict = {
        "input_olga1": "tests/olga_results/OLGA1.xlsx",
        "input_olga2": "tests/olga_results/OLGA2.xlsx",
        "input_olga3": "tests/olga_results/OLGA3.xlsx",
        "input_olga4": "tests/olga_results/OLGA4.xlsx",
        "input_olga5": "tests/olga_results/OLGA5.xlsx",
        "OLGAgaohanye": "tests\olga_results\OLGAgaohanye1.xlsx"
        }
        # olga管道案例可视化
        if current_case_name in gtpath_dict:
            
            gtpath = gtpath_dict[current_case_name]
            olga_gt_path = os.path.join(get_base_path(), gtpath)
            (gt_flow_g, gt_flow_L,
            gt_p, gt_T, gt_alpha_L,
            flow_g, flow_L,
            p, T, alpha_L) = load_olga_groundtruth_result_se(params, olga_gt_path)

            flow_g_error = error_MSE(gt_flow_g, flow_g)
            flow_L_error = error_MSE(gt_flow_L, flow_L)
            p_error = error_MSE(gt_p, p)
            T_error = error_MSE(gt_T, T)
            alpha_L_error = error_MSE(gt_alpha_L, alpha_L)
            mean_gt_flow_g = np.mean(gt_flow_g)  # olga对比值百分比
            mean_gt_flow_L = np.mean(gt_flow_L)  # olga对比值百分比
            mean_gt_p = np.mean(gt_p)  # olga对比值百分比
            mean_gt_T = np.mean(gt_T)  # olga对比值百分比
            mean_gt_alpha_L = np.mean(gt_alpha_L)  # olga对比值百分比

            # 断言
            assert flow_g_error < np.abs(mean_gt_flow_g * 0.05), "气体体积流量计算发生错误"
            assert flow_L_error < np.abs(mean_gt_flow_L * 0.05), "液体体积流量计算发生错误"
            assert p_error < np.abs(mean_gt_p * 0.05), "压力计算发生错误"
            assert T_error < np.abs(mean_gt_T * 0.02), "温度计算发生错误"
            assert alpha_L_error < np.abs(mean_gt_alpha_L * 0.2), "持液率计算发生错误"